﻿var _focusCtrl = "";
var _POPUPWIDTH = 540;
var _POPUPHEIGHT = 370;
var _LOGINPATH = "/Login.aspx";
var _isLogin = true;
var _flashWS = "/FlashWs.asmx?WSDL";

SetCID();
SetEventKey();
addLoadEvent(SetFocus);
addLoadEvent(ShowLnb);

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function ShowLnb() {
    var dv = $get("dvCategoryLNB");
    if (dv != null)
        dv.style.background = "transparent";
}

function SetCID() {
    if (typeof (Cookie) == 'undefined')
        return;

    var qry = new QueryString();
    var value = qry.GetValue("cid");

    if (value != null && value != "") {
        var cookie = new Cookie();
        var cid = cookie.Read("cid");

        if (cid != null && cid != "") {
            if (value == cid)
                return;
        }
        cookie.Save("cid", value, "/");
    }
}

function GetCID() {
    var cookie = new Cookie();
    var qry = new QueryString();
    var cid = cookie.Read("cid");

    if (cid != null && cid != '')
        return cid;

    cid = qry.GetValue("cid");

    if (cid != null && cid != 'undefined' && cid.length > 0)
        return cid;

    return null;
}

function SetEventKey() {
    if (typeof (Cookie) == 'undefined')
        return;

    var cookie = new Cookie();
    var eventKey = cookie.Read("eventKey");

    if (eventKey != null && eventKey != '')
        return;

    var qry = new QueryString();
    var value = qry.GetValue("eventKey");

    if (value == null || value == 'undefined' || value.length <= 0)
        return;

    cookie.Save("eventKey", value, "/");
}

function OnImageError(source, errorImage) {
    source.src = errorImage;
    source.onerror = "";
    return true;
}

function Popup(url, name, windowWidth, windowHeight) {
    Popup2(url, name, windowWidth, windowHeight);
}

function Popup2(url, name, windowWidth, windowHeight, enableScroll) {
    myleft = (screen.width) ? (screen.width - windowWidth) / 2 : 100; mytop = (screen.height) ? (screen.height - windowHeight) / 2 : 100; properties = "width=" + windowWidth + ",height=" + windowHeight + ",scrollbars=" + enableScroll + ", top=" + mytop + ",left=" + myleft;
    window.open(url, name, properties)
}

function openModal(url, args, w, h, scroll) {
    strReturnValue = showModalDialog(url, args, "dialogHeight:" + h + "px; dialogWidth:" + w + "px; center:Yes; help:No; resizable:yes; status:No; scroll:No");
    return strReturnValue;
}

function openWindow(url, name, w, h) {
    var opt = "width=" + w + ",height=" + h + ",top=200, left=200,scrollbars=no";
    var win = window.open(url, name, opt);

    return false;
}

function openWindowScroll(url, name, w, h) {
    var opt = "width=" + w + ",height=" + h + ",top=200, left=200, scrollbars=yes";
    window.open(url, name, opt);
    return false;
}

function SignOut() {
    var pl = new SOAPClientParameters();
    SOAPClient.invoke(_flashWS, "Logout", pl, false);
    LocationMove('/Default.aspx');
}

function LocationMove(url) {
    document.location.href = url;
}

function LocationParentMove(url) {
    if (typeof (opener) == 'undefined') {
        if (url.indexOf("http") >= 0)
            location.href = unescape(url);
        else
            location.href = _httpUrl + unescape(url);
    }
    else {
        if (typeof (opener.parent.mainframe) == 'undefined') {
            if (url.indexOf("http") >= 0)
                opener.location.href = unescape(url);
            else
                opener.location.href = _httpUrl + unescape(url);
        }
        else
            opener.top.location = "/JoinTranz.aspx?returnurl=" + url;
    }
}

function EventMove(url) {
    document.location.href = 'http://event.istyle24.com' + url;
}

function IsAuthenticated() {
    var pl = new SOAPClientParameters();
    return SOAPClient.invoke(_flashWS, "IsAuthenticated", pl, false);
}

function IsAuthenticatedSelf() {
    if (!IsAuthenticated()) {
        ShowLoginPageSelf();
        return false;
    }
    return true;
}

function IsAuthenticatedLocation(url) {
    if (!IsAuthenticated()) {
        ShowModalPageForMove(_httpsUrl + _LOGINPATH, url);
    }
    else
        location.href = url;
}

function IsAuthenticatedLocationFlash(url) {
    if (!IsAuthenticated()) {
        ShowModalPageForMove(_httpsUrl + _LOGINPATH, url);
    }
    else
        location.href = _httpUrl + url;
}

function ShowLoginPageSelf() {
    ShowModalPageForMove(_httpsUrl + _LOGINPATH, '');
}

function ShowLoginPage(url) {
    return ShowModalPageForMove(_httpsUrl + _LOGINPATH, url);
}

function ShowLoginPageAndMove(url) {
    return ShowModalPageForMove(_httpsUrl + _LOGINPATH, url);
}

// 사용금지(쓰면죽음)
function ShowModalPageForMove(dest, url) {
    if (!_isLogin) {
        alert("Trang hiện tại khách hàng không thể Đăng Nhập.");
        return;
    }

    if (url == null || url == '') {
        url = document.location.pathname + document.location.search;
    }

    //Popup(dest + "?ReturnUrl=" + escape(url) , "Login", _POPUPWIDTH, _POPUPHEIGHT);
    LocationMove(dest + "?ReturnUrl=" + escape(url));
}

function ShowGuestLoginPageAndMove(orderType) {
    location.href = _httpsUrl + "/Login.aspx?OrderType=" + orderType;
}


function GetQueryString(key) {
    var qry = new QueryString();
    return qry.GetValue(key);
}

//비회원 로그인 팝업 추가관련 스크립트 끝
function ShowModalPage(dest, url, width, height, scroll) {
    var result = openModal(dest, this, width, height, scroll)
    if (result) {
        if (url != null && url != 'undefined') {
            document.location.href = url;
            return false;
        }
        return true;
    }
    return false;
}



/***
Login Modal 의 SSL 적용으로 인한, 부모창 Redirect 처리 변경. SSL 전환으로 Login Modal 에서 window.dialogArguments 가 define 되지 않음.
2008.05.29.
비회원 로그인 팝업 추가관련 스크립트를 다음과 같이 변경 처리함.
***/
function BasketButtonClicked() {
    if (typeof (Cookie) == 'undefined')
        return ShowBasketLoginPageAndMove();

    var cookie = new Cookie();
    var guestGUID = cookie.Read("GuestGUID");

    if (guestGUID != null && guestGUID != '' && guestGUID.length > 0)
        return true;
    else if (!IsAuthenticated())
        return ShowBasketLoginPageAndMove();
    else
        return true;
}

//상품상세 이미지 바뀌기
function changeImgOver(imgpath, img) {
    var ctrl = document.getElementById(img);
    ctrl.src = imgpath;
}

function changeImgOut(imgpath, img) {
    var ctrl = document.getElementById(img);
    ctrl.src = imgpath;
}

function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function validateEmailv2(email) {
    if (email.length <= 0) {
        return true;
    }
    var splitted = email.match("^(.+)@(.+)$");
    if (splitted == null) return false;
    if (splitted[1] != null) {
        var regexp_user = /^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null)
            return false;
    }
    if (splitted[2] != null) {
        var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if (splitted[2].match(regexp_domain) == null) {
            var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if (splitted[2].match(regexp_ip) == null) return false;
        }
        return true;
    }
    return false;
}


function validateForm(arrObjDesc) {
    for (var i = 0; i < arrObjDesc.length; i++) {
        for (var j = 0; j < arrObjDesc[i].length; j++) {
            //var objForm = document.all[arrObjDesc[i][j][0]];
            var objForm = document.getElementById(arrObjDesc[i][j][0]);

            if (validateData(objForm.value, arrObjDesc[i][j][1], arrObjDesc[i][j][2]) == false) {
                if (objForm.type != "hidden") {
                    objForm.focus();
                }
                event.returnValue = false;
                return false;
            }
        }
    }
    return true;
}

function validateValueForm(arrObjDesc) {
    for (var i = 0; i < arrObjDesc.length; i++) {
        for (var j = 0; j < arrObjDesc[i].length; j++) {
            var objForm = $(arrObjDesc[i][j][0]);
            var objValue = arrObjDesc[i][j][1];

            if (validateData(objValue, arrObjDesc[i][j][2], arrObjDesc[i][j][3]) == false) {
                if (objForm != null) {
                    objForm.focus();
                    //objForm.select();
                }
                //event.returnValue = false;
                return false;
            }
        }
    }
    return true;
}

function validateData(objValue, strValidateStr, strError) {
    var epos = strValidateStr.search("=");
    var command = "";
    var cmdvalue = "";

    if (epos >= 0) {
        command = strValidateStr.substring(0, epos);
        cmdvalue = strValidateStr.substr(epos + 1);
    }
    else {
        command = strValidateStr;
    }

    switch (command) {
        case "req":
        case "required":
            {
                var tp = trim(objValue).replace(/\s/g, '');
                if (eval(tp.length) == 0) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "reqboth":
            {
                var tp = trim(objValue).replace(/\s/g, '');
                var tp1 = trim(cmdvalue).replace(/\s/g, '');

                if (eval(tp.length) == 0 && eval(tp1.length) == 0) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "maxlen":
        case "maxlength":
            {
                if (eval(objValue.length) > eval(cmdvalue)) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "minlen":
        case "minlength":
            {
                if (eval(objValue.length) < eval(cmdvalue) && eval(objValue.length) != eval(0)) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "equallen":
        case "equallength":
            {
                if (eval(objValue.length) != eval(cmdvalue)) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "equalObjectValue":
            {
                if (objValue != document.all[cmdvalue].value) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "equalvalue":
            {
                if (objValue != cmdvalue) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "equaldeny":
            {
                if (objValue == cmdvalue) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "eng":
        case "english":
            {
                var charpos = objValue.search("[^A-Za-z]");
                if (objValue.length > 0 && charpos >= 0) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "engnum":
        case "engnumeric":
            {
                var charpos = objValue.search("[^A-Za-z0-9]");
                if (objValue.length > 0 && charpos >= 0) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "engnumhyphen":
            {
                var charpos = objValue.search("[^A-Za-z0-9\-_]");
                if (objValue.length > 0 && charpos >= 0) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "num":
        case "numeric":
            {
                var charpos = objValue.search("[^0-9\]");
                if (objValue.length > 0 && charpos >= 0) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "float":
            {
                if (objValue != undefined) {
                    var charpos = objValue.search("[^0-9\-.]");
                    if (objValue.length > 0 && charpos >= 0) {
                        alert(strError);
                        return false;
                    }
                }
                break;

            }
        case "numhyphen":
            {
                var charpos = objValue.search("[^0-9\-_]");
                if (objValue.length > 0 && charpos >= 0) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "email":
            {
                if (!validateEmailv2(objValue)) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "email2":
            {
                if (objValue.indexOf("@", 0) != -1) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "lt":
        case "lessthan":
            {
                if (isNaN(objValue)) {
                    return false;
                }
                if (eval(objValue) >= eval(cmdvalue)) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "gt":
        case "greaterthan":
            {
                if (isNaN(objValue)) {
                    return false;
                }
                if (eval(objValue) <= eval(cmdvalue)) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "regexp":
            {
                if (objValue.length > 0) {
                    if (!objValue.match(cmdvalue)) {
                        alert(strError);
                        return false;
                    }
                }
                break;
            }
        case "dontselect":
            {
                if (objValue.selectedIndex == null) {
                    return false;
                }
                if (objValue.selectedIndex == eval(cmdvalue)) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "filecheck":
            {
                if (objValue.length > 0) {
                    var index = objValue.lastIndexOf(".");
                    var strExt = objValue.substr(index);

                    if (strExt.toLowerCase() != ".jpg" && strExt.toLowerCase() != ".gif" && strExt.toLowerCase() != ".png") {
                        alert(strError)
                        return false;
                    }
                }
                break;
            }
        case "checked":
            {
                if (objValue == false) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "listbox":
            {
                if (objValue.all[0] == null) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "ipaddress":
            {
                var ipaddress = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
                var check = ipaddress.exec(objValue);

                if (!check) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "port":
            {
                var port = /^(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9]|[0-5]?[0-9]?[0-9]?[0-9]?[0-9])$/;
                var check = port.exec(objValue);

                if (!check) {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "url":
            {
                var ser = "http://";
                var str = objValue.toLowerCase();

                if (str == "")
                    return true;


                if (str.search(ser) == -1) {
                    alert(strError)
                    return false;
                }
                else {
                    return true;
                }
                break;
            }
        case "optionlength":
            {
                if (objValue.length > 0) {
                    return true;
                }
                else {
                    alert(strError);
                    return false;
                }
                break;
            }
        case "none":
            {
                return true;
                break;
            }
    }
    return true;
}

//**************************************** 

//*** 입력된 문자열이 주민등록번호인지 체크 

//**************************************** 

function isJuminNum(aNum1, aNum2) {
    var tot = 0, result = 0, re = 0, se_arg = 0;
    var chk_num = "";
    var aNum = aNum1 + aNum2;

    if (aNum.length != 13) {
        return false;
    }
    else {
        for (var i = 0; i < 12; i++) {
            if (isNaN(aNum.substr(i, 1)))
                return false;
            se_arg = i;


            //외국인 인 경우 
            if (i == 6) {
                if (aNum.substr(i, 1) == 7 || aNum.substr(i, 1) == 8)
                    return true
            }

            if (i >= 8)
                se_arg = i - 8;
            tot = tot + Number(aNum.substr(i, 1)) * (se_arg + 2)
        }

        if (chk_num != "err") {
            re = tot % 11;
            result = 11 - re;
            if (result >= 10) result = result - 10;
            if (result != Number(aNum.substr(12, 1))) return false;
            if ((Number(aNum.substr(6, 1)) < 1) || (Number(aNum.substr(6, 1)) > 4)) return false;
        }
    }
    return true;
}

function SetFocus() {
    if (_focusCtrl == "")
        return;

    var o = $get(_focusCtrl);
    if (o != null)
        o.focus();
}


/************** 고객센터에서 사용(향후 삭제처리 대상) ***********************/
function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p, i, x;
    if (!d) d = document;
    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all)
        x = d.all[n];
    for (i = 0; !x && i < d.forms.length; i++)
        x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++)
        x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById)
        x = d.getElementById(n);
    return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

//상품상세 문의 제목 클릭

var old_menu_know = new Array('', '');

function know_menuclick(num, menu_depth) {
    var submenu = document.getElementById(num);

    if (old_menu_know[menu_depth] != submenu) {
        if (old_menu_know[menu_depth] != '') old_menu_know[menu_depth].style.display = 'none';
        submenu.style.display = 'block';
        old_menu_know[menu_depth] = submenu;
    }
    else {
        submenu.style.display = 'none';
        old_menu_know[menu_depth] = '';
    }
}

function LocationMoveLogin(url) {
    //  수정 : 접근 파라미터 처리에 문제. 처리완료.
    var jsurl;
    var jsLocationPathName = '';
    var lSearch;

    jsLocationPathName = location.pathname;

    if (jsLocationPathName != '/Member/MemberJoin01.aspx') {
        lSearch = document.location.search;

        lSearch = lSearch.replace("?", "");
        lSearch = lSearch.replace("&", "_");
        lSearch = lSearch.replace("=", ":");
        lSearch = lSearch.replace("=", ":");

        // url += "?purl0=" + jsLocationPathName + "&purl1=" + encodeURIComponent(lSearch);
        url += "?purl0=" + jsLocationPathName + "&purl1=" + lSearch;
    }

    document.location.href = url;
}

////////////////////// 오늘 본 상품 레이어 스크립트 /////////////
self.onError = null;
var lastScrollY = 0;
var NS = (document.layers) ? true : false;
var IE = (document.all) ? true : false;

function heartBeat() {
    // 2007.06.21 
    // 스카이 스크랩퍼 지정 범위내 이동 테스트 중
    // 주석 처리 부분들 삭제하지 말아주세요.

    try {
        var diffY;

        if (IE) {
            diffY = document.documentElement.scrollTop;
        }

        if (NS) {
            diffY = self.pageYOffset;
        }

        if (diffY != lastScrollY) {
            percent = .1 * (diffY - lastScrollY);

            if (percent > 0)
                percent = Math.ceil(percent);
            else
                percent = Math.floor(percent);

            if (IE) {
                document.all.dvSkyScraper.style.pixelTop += percent;
            }

            if (NS) {
                document.dvSkyScraper.top += percent;
            }

            lastScrollY = lastScrollY + percent;
        }
    }
    catch (e) {
    }
}
/////////////////////////////////////////////////////////////////

function getURL(url0, url1) {
    //  회원완료 페이지에서 사용함.
    var rtnURL;
    // return url0 + '?' + decodeURIComponent(url1);
    if (url0 != "") {
        if (url1 != "") {
            url1 = url1.replace("_", "&").replace(":", "=").replace(":", "=");

            rtnURL = url0 + "?" + url1;
        }
        else {
            rtnURL = url0;
        }
    }
    else {
        rtnURL = "/Default.aspx";
    }

    document.location.href = rtnURL;
}

function MM_showHideLayers() { //v3.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v = 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}
